%%% Copyright 2010 Google Inc.
%%%
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%%     http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.

%%% @copyright 2010 Google Inc. All Rights Reserved.
%%% @author <jacoby@google.com> Jacoby Thwaites [http://sites/madcloud]
%%% @version {@version}
%%% @doc
%%% Tests for web_util module.

-module(web_util_tests).

%% Include eunit header.
-include_lib("eunit/include/eunit.hrl").

%% Test string to term parsing.
list_to_term_test() ->
  ?assertEqual({a, 2, b}, web_util:list_to_term("{a,2,b}")).

%% Test URL decoding.
url_decode_test() ->
  ?assertEqual("", web_util:url_decode("")),
  ?assertEqual("nothing to do", web_util:url_decode("nothing to do")),
  ?assertEqual("something to do", web_util:url_decode("something%20to%20do")),
  ?assertEqual("something to do", web_util:url_decode("something+to+do")),
  ?assertEqual("e f g", web_util:url_decode("%65+%66+%67")).

%% Test querystring decoding.
querystring_test() ->
  Props = web_util:query2proplist("%66oo=10&b%61r=20&baz=some+value%20here"),
  ?assertEqual("10", proplists:get_value("foo", Props)),
  ?assertEqual("20", proplists:get_value("bar", Props)),
  ?assertEqual("some value here", proplists:get_value("baz", Props)).

%% Constants used in multipart/form-data tests.
-define(BOUNDARY, "AaB03x").
-define(MULTIPART_DATA,
    "--AaB03x\r\n"
      "Content-Disposition: form-data; name=\"submit-name\"\r\n"
        "\r\n"
          "Larry\r\n"
            "--AaB03x\r\n"
              "Content-Disposition: form-data; name=\"files\"; "
                "filename=\"file1.txt\"\r\n"
                  "Content-Type: text/plain\r\n"
                    "\r\n"
                      "File Contents\r\n"
                        "--AaB03x--\r\n").

%% Test splitting multipart/form-data into a list of structured parts.
parts_test() ->
  Parts = web_util:parts(?MULTIPART_DATA, ?BOUNDARY),
  ?assertEqual(2, length(Parts)),

  % Check the parts have been correctly parsed.
  [Part1, Part2] = Parts,
  ?assertEqual({[{"Content-Disposition",
                  {"form-data",
                   [{"name","submit-name"}]}}],
                "Larry"},
               Part1),
  ?assertEqual({[{"Content-Disposition",
                  {"form-data",
                   [{"name", "files"}, {"filename", "file1.txt"}]}},
                 {"Content-Type", {"text/plain", []}}],
                "File Contents"},
               Part2).

%% Test retrieval of form-data part by name attribute is correct.
form_data_test() ->
  Parts = web_util:parts(?MULTIPART_DATA, ?BOUNDARY),
  ?assertEqual("Larry", web_util:form_data(Parts, "submit-name")),
  ?assertEqual("File Contents", web_util:form_data(Parts, "files")).
